# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        博智科技  
# Name:         demo0831
# Description:  __new__
# Author:       yzl
# Date:         2019-02-22
#-------------------------------------------------------------------------------

class Person:

    def __new__(cls, name,age):
        print('__new__ called')
        # 必须要有返回值
        return super(Person,cls).__new__(cls)

    def __init__(self,name,age):
        print('__init__ called')
        self.name = name
        self.age = age

    def __str__(self):
        return '<Persion:%s(%s)' % (self.name,self.age)

class P1(int):
    def __init__(self,value):
        super(P1,self).__init__(self,abs(value))

class P2(int):
    def __new__(cls,value):
        return super(P2,cls).__new__(cls,abs(value))

# 单例示例
class Singleton:
    instance = None

    def __new__(cls,xx,yy):
        if cls.instance is None:
            cls.instance = super().__new__(cls)
        return cls.instance

    def __init__(self,xx,yy):
        self.xx = xx
        self.yy = yy


if __name__ == '__main__':
    p = Person('p',22)
    print(p)

    # p1 = P1(-5)
    # print(p1)

    p2 = P2(-5)
    print(p2)

    obj1 = Singleton(1,2)
    obj2 = Singleton(2,1)

    print(obj1.xx)
    print(obj2.xx)

    print(obj1 is obj2)